import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams, ActionSheetController, App, Tab } from 'ionic-angular';
import { BusiProvider } from '../../providers/busi/busi';
import { Camera, CameraOptions } from '@ionic-native/camera';
import { FileTransfer, FileUploadOptions, FileTransferObject } from '@ionic-native/file-transfer';
import { Global } from '../../common/global';
import { NativeProvider } from '../../providers/native/native';

/**
 * Generated class for the ProductaddPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@IonicPage()
@Component({
  selector: 'page-productadd',
  templateUrl: 'productadd.html',
})
export class ProductaddPage {
  imgpath: any;
  serverImgPath:any;
  showImgPath:any;

  title:any;
  type:any;
  price:any;
  desc;
  fileid:any;
  constructor(public navCtrl: NavController,
    public navParams: NavParams,
    public busi: BusiProvider,
    private camera: Camera,
    public actionSheetCtrl: ActionSheetController,
    private transfer: FileTransfer,
    private native:NativeProvider,
    private app:App) {
  }

  ionViewDidLoad() {
    console.log('ionViewDidLoad ProductaddPage');
  }

  presentActionSheet() {
    const actionSheet = this.actionSheetCtrl.create({
      title: "选择照片",
      buttons: [
        {
          text: "拍摄",
          handler: () => {
            this.getPic(1);
          }
        },
        {
          text: "从相册选择",
          handler: () => {
            this.getPic(2);
          }
        }
      ]
    });
    actionSheet.present();
  }

  getPic(flag) {
    const options: CameraOptions = {
      quality: 100,
      destinationType: this.camera.DestinationType.FILE_URI,
      encodingType: this.camera.EncodingType.JPEG,
      mediaType: this.camera.MediaType.PICTURE,
      saveToPhotoAlbum: false,
      sourceType:
        flag == "2"
          ? this.camera.PictureSourceType.SAVEDPHOTOALBUM
          : this.camera.PictureSourceType.CAMERA
    };

    this.camera.getPicture(options).then(
      imageData => {
        this.imgpath = imageData;
        this.imgUpload();
      },
      err => {}
    );
  }
  imgUpload() {
    const fileTransfer: FileTransferObject = this.transfer.create();
    let options: FileUploadOptions = {
      fileKey: "file",
      fileName: "name.jpg",
      headers: {}
    };
    fileTransfer
      .upload(this.imgpath, Global.base_url + "upload/fileupload", options)
      .then(
        data => {
          this.serverImgPath = JSON.parse(data.response).result.filepath;
          this.fileid = JSON.parse(data.response).result.fileid;
          this.showImgPath =
          Global.base_url + JSON.parse(data.response).result.filepath;
        },
        err => {
          console.log(err);
        }
      );
  }
  launch(){
    let body={
      title:this.title,
      desc:this.desc,
      price:this.price,
      type:this.type,
      imgid:this.fileid
    }
    this.busi.addProd(body).subscribe(
      data=>{
        this.native.showToast("发布成功，等待审核!");
        this.app.getActiveNavs()[0].parent.select(0);
      },
      err=>{

      }
    );
  }

}
